/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pracamagisterska;

import java.awt.Color;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.util.SortOrder;

/**
 *
 * @author Adrian
 */
public class Statistic extends ModalityInternalFrame {

    private org.jbundle.thin.base.screen.jcalendarbutton.JCalendarButton jCalendarButton1;
    private org.jbundle.thin.base.screen.jcalendarbutton.JCalendarButton jCalendarButton2;
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    DatabaseThread db;

    /**
     * Creates new form Statistic
     */
    public Statistic(JComponent parent, DatabaseThread db) {
        super(parent, db);
        this.db = db;
        initComponents();
        initCalendars();
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        setClosable(true);
        jTextField1.setText(sdf.format(cal.getTime()));
        jTextField2.setText(sdf.format(cal.getTime()));
    }

    private void initCalendars() {
        jCalendarButton1 = new org.sourceforge.jcalendarbutton.JCalendarButton();
        jCalendarButton1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            @Override
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                if (evt.getNewValue() instanceof Date) {
                    setDate((Date) evt.getNewValue());
                }
            }

            private void setDate(Date date) {
                jTextField1.setText(dateFormat.format(date));
            }
        });
        this.add(jCalendarButton1);
        jCalendarButton1.setLocation(150, 100);
        jCalendarButton1.setSize(34, 34);
        jCalendarButton1.setVisible(true);
        //--- drugi przycisk
        jCalendarButton2 = new org.sourceforge.jcalendarbutton.JCalendarButton();
        jCalendarButton2.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            @Override
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                if (evt.getNewValue() instanceof Date) {
                    setDate((Date) evt.getNewValue());
                }
            }

            private void setDate(Date date) {
                jTextField2.setText(dateFormat.format(date));
            }
        });
        this.add(jCalendarButton2);
        jCalendarButton2.setLocation(150, 155);
        jCalendarButton2.setSize(34, 34);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();

        setToolTipText("");

        jLabel1.setText("Od:");

        jTextField1.setEditable(false);

        jLabel2.setText("Do:");

        jTextField2.setEditable(false);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Słupkowy", "Kołowy" }));

        jLabel3.setText("Rodzaj wykresu:");

        jButton3.setText("Bieżacy miesiąc");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Bieżacy rok");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/childish_calculator36.png"))); // NOI18N
        jButton5.setText("                       Liczba gości");
        jButton5.setToolTipText("Liczba gości przebywającyh w hotelu w danym okresie");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/childish_globe36.png"))); // NOI18N
        jButton6.setText("            Pochodzenie gości");
        jButton6.setToolTipText("Narodowość zameldowanych gości w danym okresie");
        jButton6.setActionCommand("              Pochodzenie gości");
        jButton6.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/childish_home36.png"))); // NOI18N
        jButton7.setText("            Sprzedane pokoje");
        jButton7.setToolTipText("Ilość sprzedanych pokoii w danym okresie");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/childish_cross36.png"))); // NOI18N
        jButton8.setText("                           Zamknij");
        jButton8.setToolTipText("Ilość sprzedanych pokoii w danym okresie");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1)
                            .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                            .addComponent(jSeparator1)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                                .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jButton6.getAccessibleContext().setAccessibleName("       Pochodzenie gości");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        int x = cal.get(cal.MONTH) + 1;
        jTextField1.setText(cal.get(cal.YEAR) + "-" + (cal.get(cal.MONTH) + 1) + "-01");
        jTextField2.setText(cal.get(cal.YEAR) + "-" + (cal.get(cal.MONTH) + 1) + "-" + cal.getActualMaximum(cal.DAY_OF_MONTH));
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        Calendar cal = Calendar.getInstance();
        jTextField1.setText(cal.get(cal.YEAR) + "-01-01");
        jTextField2.setText(cal.get(cal.YEAR) + "-12-31");
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (sdf.parse(jTextField1.getText()).compareTo(sdf.parse(jTextField2.getText())) > 0) {
                jTextField2.setBackground(Color.YELLOW);
                jTextField2.setToolTipText("Data do nie może być przed datą od !");
            } else {
                jTextField2.setBackground(Color.white);
                jTextField2.setToolTipText("");
                ModalityInternalFrame ch = null;
                if (jComboBox1.getSelectedIndex() == 0) {//slupkowy
                    CategoryDataset dataset = createCategoryDataset(1);
                    ch = new Chart(this, dataset, "Ilość gości w danym dniu","Data");
                }
                if (jComboBox1.getSelectedIndex() == 1) { //kolowy
                    PieDataset dataset = createPieDataset(1);
                    ch = new Chart(this, dataset, "Ilość gości w danym dniu","Data");
                }
                JDesktopPane desktop = JOptionPane.getDesktopPaneForComponent(this);
                desktop.add(ch);
                ch.show();
            }
        } catch (ParseException ex) {
            Logger.getLogger(Statistic.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (sdf.parse(jTextField1.getText()).compareTo(sdf.parse(jTextField2.getText())) > 0) {
                jTextField2.setBackground(Color.YELLOW);
                jTextField2.setToolTipText("Data do nie może być przed datą od !");
            } else {
                jTextField2.setBackground(Color.white);
                jTextField2.setToolTipText("");
                ModalityInternalFrame ch = null;
                if (jComboBox1.getSelectedIndex() == 0) {//slupkowy
                    CategoryDataset dataset = createCategoryDataset(2);
                    ch = new Chart(this, dataset, "Kraj pochodzenia gości hotelu w okresie od " + jTextField1.getText() + " do " + jTextField2.getText(),"Kraj");
                }
                if (jComboBox1.getSelectedIndex() == 1) { //kolowy
                    PieDataset dataset = createPieDataset(2);
                    ch = new Chart(this, dataset, "Kraj pochodzenia gości hotelu w okresie od " + jTextField1.getText() + " do " + jTextField2.getText(),"Kraj");
                }
                JDesktopPane desktop = JOptionPane.getDesktopPaneForComponent(this);
                desktop.add(ch);
                ch.show();
            }
        } catch (ParseException ex) {
            Logger.getLogger(Statistic.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (sdf.parse(jTextField1.getText()).compareTo(sdf.parse(jTextField2.getText())) > 0) {
                jTextField2.setBackground(Color.YELLOW);
                jTextField2.setToolTipText("Data do nie może być przed datą od !");
            } else {
                ModalityInternalFrame ch = null;
                if (jComboBox1.getSelectedIndex() == 0) {//slupkowy
                    CategoryDataset dataset = createCategoryDataset(3);
                    ch = new Chart(this, dataset, "Ilość sprzedanych pokojodób w okresie od " + jTextField1.getText() + " do " + jTextField2.getText() + " dla wskazanego pokoju","Nr pokoju");
                }
                if (jComboBox1.getSelectedIndex() == 1) { //kolowy
                    PieDataset dataset = createPieDataset(3);
                    ch = new Chart(this, dataset, "Ilość sprzedanych pokojodób w okresie od " + jTextField1.getText() + " do " + jTextField2.getText() + " dla wskazanego pokoju","Nr pokoju");
                }
                JDesktopPane desktop = JOptionPane.getDesktopPaneForComponent(this);
                desktop.add(ch);
                ch.show();
            }
        } catch (ParseException ex) {
            Logger.getLogger(Statistic.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jButton7ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        this.dispose();
        if (parent != null && parent instanceof ModalityInternalFrame) {
            ((ModalityInternalFrame) parent).childClosing();
        }
    }//GEN-LAST:event_jButton8ActionPerformed

    private CategoryDataset createCategoryDataset(int type) {
        DefaultCategoryDataset result2 = new DefaultCategoryDataset();
        /*if (type == 1) { // ilosc gosci
            Map<String, Integer> datalist = db.Statistic_getGuestNumber(jTextField1.getText(), jTextField2.getText());
            Iterator iterator = datalist.entrySet().iterator();
            System.out.println(iterator.toString());
            while (iterator.hasNext()) {
                Entry entry = (Entry) iterator.next();
                result.setValue(Double.parseDouble(entry.getValue().toString()), "Data", entry.getKey().toString());
            }
        } else if (type == 2) { //pochodzenie gosci
            Map<String, Integer> datalist = db.Statistic_getGuestOrigin(jTextField1.getText(), jTextField2.getText());
            Iterator iterator = datalist.entrySet().iterator();
            System.out.println(iterator.toString());
            while (iterator.hasNext()) {
                Entry entry = (Entry) iterator.next();
                result.setValue(Double.parseDouble(entry.getValue().toString()), "Kraj pochodzenia", entry.getKey().toString());
            }
        } else if (type == 3) {
            Map<String, Integer> datalist = db.Statistic_getRentedRoom(jTextField1.getText(), jTextField2.getText());
            Iterator iterator = datalist.entrySet().iterator();
            System.out.println(iterator.toString());
            while (iterator.hasNext()) {
                Entry entry = (Entry) iterator.next();
                result.setValue(Double.parseDouble(entry.getValue().toString()), "Numer pokoju", entry.getKey().toString());
            }
        }*/
        DefaultPieDataset result = (DefaultPieDataset) createPieDataset(type);        
        for (int i = 0; i < result.getItemCount(); i++) {
            result2.setValue(Double.parseDouble(result.getValue(i).toString()), "", result.getKey(i).toString());
        }
        return result2;
    }

       
    private PieDataset createPieDataset(int type) {
        DefaultPieDataset result = new DefaultPieDataset();
        if (type == 1) { // ilosc gosci
            Map<String, Integer> datalist = db.Statistic_getGuestNumber(jTextField1.getText(), jTextField2.getText());
            Iterator iterator = datalist.entrySet().iterator();
            while (iterator.hasNext()) {
                Entry entry = (Entry) iterator.next();
                result.setValue(entry.getKey().toString(), Integer.parseInt(entry.getValue().toString()));
            }
        } else if (type == 2) { //pochodzenie gosci
            Map<String, Integer> datalist = db.Statistic_getGuestOrigin(jTextField1.getText(), jTextField2.getText());
            Iterator iterator = datalist.entrySet().iterator();
            while (iterator.hasNext()) {
                Entry entry = (Entry) iterator.next();
                result.setValue(entry.getKey().toString(), Integer.parseInt(entry.getValue().toString()));
            }
        } else if (type == 3) {
            Map<String, Integer> datalist = db.Statistic_getRentedRoom(jTextField1.getText(), jTextField2.getText());
            Iterator iterator = datalist.entrySet().iterator();
            while (iterator.hasNext()) {
                Entry entry = (Entry) iterator.next();
                result.setValue(entry.getKey().toString(), Integer.parseInt(entry.getValue().toString()));
            }            
        }
        result.sortByKeys(SortOrder.ASCENDING);
        return result;

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
